import Toast from '@/components/Toast/src'

// 数据校验方法封装
function verfiyData(arr:any){   
  const promise = new Promise(function(resolve,reject){
    const reg = arr.checkRule
    const verfiy = reg.test(arr.value)
    if(verfiy==false){
      reject(false)
    }else{
      resolve(true)
    }
  })
  return promise
}

const verfiyMethod = (arr:any)=>{
 const promises = new Promise(function(resolve,reject){
    let timeTrue = 0
    let timeFalse = 0
    for(const key in arr){
      verfiyData(arr[key]).then((res)=>{
        timeTrue++
        if(timeTrue>=Object.values(arr).length){
          console.log('数据校验完成')
          resolve(res)
        }
      }).catch((err)=>{
        timeFalse++
        if(timeFalse == 1){
          Toast(arr[key].errorMsg+arr[key].msg)
          reject(err)
        }
      })
    }
 })
 return promises
}


export default {
  verfiyMethod
}